package com.qqb.controller;

import com.github.pagehelper.Page;
import com.qqb.pojo.Company;
import com.qqb.pojo.Result;
import com.qqb.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @PostMapping("/searchPageList/{pageNum}/{pageSize}")
    public Map<String, Object> searchPageList(@RequestBody(required = false) Map<String, String> condition,
                                              @PathVariable String pageNum,
                                              @PathVariable String pageSize) {
        if (pageNum == null || "".equals(pageNum)) {
            pageNum = "1";
        }
        if (pageSize == null || "".equals(pageSize)) {
            pageNum = "3";
        }
        Page<Company> pageList = companyService.searchPageList(condition, pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageDate", pageList);
        map.put("totalNum", pageList.getTotal());
        return map;
    }

    @PostMapping("/add")
    public Result add(@RequestBody Company company) throws Exception {
        companyService.add(company);
        return new Result();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Company company) throws Exception {
        companyService.update(company);
        return new Result();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody String[] ids){
        companyService.delete(ids);
        return new Result();
    }

}
